﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using HundredMilesSoftware.UltraID3Lib;

namespace HebrewID3Fix
{
    public partial class FormMain : Form
    {
		List<FileInfo> m_oDamagedFiles;

		CHebrewMP3Fixer m_oFixer = new CHebrewMP3Fixer();

        public FormMain()
        {
            InitializeComponent();

			ColumnHeader l_oHeader;
			l_oHeader = new ColumnHeader();
			l_oHeader.Text = "Filename";
			listView1.Columns.Add(l_oHeader);
			l_oHeader = new ColumnHeader();
			l_oHeader.Text = "Genre";
			listView1.Columns.Add(l_oHeader);
			l_oHeader = new ColumnHeader();
			l_oHeader.Text = "Artist";
			listView1.Columns.Add(l_oHeader);
			l_oHeader = new ColumnHeader();
			l_oHeader.Text = "Album";
			listView1.Columns.Add(l_oHeader);
			l_oHeader = new ColumnHeader();
			l_oHeader.Text = "Title";
			listView1.Columns.Add(l_oHeader);

			textBoxPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
        }

		private void PrintInfo(string a_sString)
		{
		}

		private void buttonBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog l_oDialog = new FolderBrowserDialog();
            l_oDialog.SelectedPath = textBoxPath.Text;

            if (l_oDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxPath.Text = l_oDialog.SelectedPath;
            }
        }

		private void AddBytesToListView(string a_sName, byte[] a_abBytes)
		{
			ListViewItem l_oItem = new ListViewItem();

			l_oItem.Text = a_sName;

			foreach (byte l_bByte in a_abBytes)
				l_oItem.SubItems.Add(l_bByte.ToString("X2"));
			
			listView1.Items.Add(l_oItem);
		}

		private void buttonFix_Click(object sender, EventArgs e)
		{
			foreach (FileInfo l_oFile in m_oDamagedFiles)
			{
				m_oFixer.FixFile(l_oFile.FullName);
			}

			m_oDamagedFiles.Clear();
			RefreshListView();
		}

		private void ScanDirectoryForProblems(string a_sPath, bool a_bRecurse)
		{
			DirectoryInfo l_oDirInfo = new DirectoryInfo(a_sPath);

			// Scan inner directories
			if (a_bRecurse)
			{
				foreach (DirectoryInfo l_oInnerDir in l_oDirInfo.GetDirectories())
				{
					ScanDirectoryForProblems(l_oInnerDir.FullName, a_bRecurse);
				}
			}

			// Find damaged files
			foreach (FileInfo l_oFile in l_oDirInfo.GetFiles("*.mp3"))
			{
				if (m_oFixer.FileDamaged(l_oFile.FullName))
					m_oDamagedFiles.Add(l_oFile);
			}
		}

		private void buttonScan_Click(object sender, EventArgs e)
		{
			string l_sPath = textBoxPath.Text;
			m_oDamagedFiles = new List<FileInfo>();

			ScanDirectoryForProblems(l_sPath, true);

			RefreshListView();
		}

		private void RefreshListView()
		{
			listView1.Items.Clear();

			foreach (FileInfo l_oFile in m_oDamagedFiles)
			{
				UltraID3 l_oTag = new UltraID3();
				l_oTag.Read(l_oFile.FullName);
				
				ListViewItem l_oItem = new ListViewItem(new String[] {l_oFile.FullName, l_oTag.Genre, l_oTag.Artist, l_oTag.Album, l_oTag.Title});
				listView1.Items.Add(l_oItem);
			}

			listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
		}

		private void buttonFix_Click_1(object sender, EventArgs e)
		{

		}

    }
}
